package com.qianbei.common.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qianbei.R;
import com.qianbei.common.LoadMoreListView;
import com.qianbei.common.adapter.CommonAdapter;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.ProgressView;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by user on 16/11/17.
 */

public abstract class NewBaseListViewFragment<T> extends BaseFragment implements LoadMoreListView.OnLoadMoreListViewListener {

    protected LoadMoreListView mLoadMoreListView;
    protected CommonAdapter mListViewAdapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    // 下一页显示页码，小于零则表示没有更多数据了
    // 下拉刷新
    protected   int mPage = 1;

    private View  nodata_view;
    private Fragment nodata_fragment;

    protected int  mPTagType;//标记 页面
    private ProgressView mProgressView;




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mPage = 1;
        return inflater.inflate(inflaterLayout(),container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mProgressView = (ProgressView) view.findViewById(R.id.progress);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                requestData(false);
            }
        });

        nodata_view=view.findViewById(R.id.nodata_view);
        nodata_fragment= getChildFragmentManager().findFragmentById(
                R.id.nodata_fragment);
        mLoadMoreListView = (LoadMoreListView) view.findViewById(R.id.load_list_view);
        mLoadMoreListView.setOnLoadMoreListViewListener(this);
        mListViewAdapter = getAdapter();//new Home_user_Adapter(this);
        mLoadMoreListView.setAdapter(mListViewAdapter);

        findView();

        requestData(true); //请求数据 接口
    }

    public void findView(){

    }

    /**
     * 请求 数据接口交给 子类处理
     *
     * @param isFirst
     */


    public void requestData(boolean isFirst) {
        ServerControl sc = getServerControl();
        sc.serverListener = new ServerControl.ServerListener() {
            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {

                    ArrayList<T> list = getList(sr.bodyData);

                    if (list != null) {
                        if (list.size() == 0) {
                            if (mPage == 1){
                                mListViewAdapter.clear();
                            }
                            mPage = -1;
                            setNodataViewVisable();

                        } else if (list.size() > 0) {
                            setNodataViewGone();
                            if (mPage == 1) {
                                mListViewAdapter.clear();
                            }

                            getPage(sr.bodyData);
                            mListViewAdapter.addAll(list);
                        }
                    }

                    resetRefresh();
                }else{
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }

        };

        if (isFirst) {
            mProgressView.startControl(sc);
        } else {
            sc.startVolley();
        }

    }


    public void getPage(org.json.JSONObject bodyData) {
        int id = bodyData.optInt("total_page");
        Log.i("jiao"," getPage "+mPage +"  id "+id);
        if (mPage >= id) {
            mPage = -1;
        } else {
            mPage = mPage + 1;
        }
    }

    /**
     * 重置刷新状态
     */
    public void resetRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);

        if (mPage < 0) {
            mLoadMoreListView.noMoreData();
        } else {
            mLoadMoreListView.showLoadMore();
        }
    }

    @Override
    public void onLoadMore() {
        if (mPage > 0) {
            requestData(false);
        }
    }


    /**
     *
     * 加载布局
     *
     */

    public abstract int inflaterLayout();

    /**
     * 解析 数据
     *
     * @param body
     * @return
     */
    public abstract ArrayList<T> getList(JSONObject body);

    /**
     * 请求 的 Serverc
     *
     * @return
     */
    public abstract ServerControl getServerControl();


    /**
     * 返回listView adapter
     *
     * @return
     */
    public abstract CommonAdapter getAdapter();


    /**
     * 没有数据 显示 的 页面
     *
     */
    public void  setNodataViewVisable(){
        if(mPTagType==2){
            nodata_view.setVisibility(View.VISIBLE);
            nodata_fragment.setUserVisibleHint(true);
        }


    }
    public void  setNodataViewGone(){
        if(mPTagType==2){
            nodata_view.setVisibility(View.GONE);
            nodata_fragment.setUserVisibleHint(false);
        }
    }

}
